#include "TransparentStatic.h"

CTransparentStatic::CTransparentStatic(void)
	: m_sibling(NULL)
	, m_rgbText(RGB(0, 0, 0))
	, m_font(NULL)
	, m_align(CTransparentStatic::CENTER)
	, m_icon(NULL)
{
}

CTransparentStatic::~CTransparentStatic(void)
{
	if (m_icon) {
		::DestroyIcon(m_icon);
		m_icon = NULL;
	}
}

void CTransparentStatic::setSibling(ATL::CWindow *sibling) {
	ATLASSERT (sibling);
	m_sibling = sibling;
}

COLORREF CTransparentStatic::setTextColor(COLORREF rgbText) {
	COLORREF oldColor = m_rgbText;
	m_rgbText = rgbText;

	if (IsWindow()) {
		UpdateWindow();
	}

	return oldColor;
}

void CTransparentStatic::setAlign(CTransparentStatic::Align align) {
	if (align != m_align) {
		m_align = align;

		onSetAlign();

		if (IsWindow()) {
			UpdateWindow();
		}
	}
}

void CTransparentStatic::setIcon(HICON icon) {
	if (m_icon != icon) {
		if (m_icon) {
			::DestroyIcon(m_icon);
		}

#if 0 // ensure icon size when painting
		if (icon) {
			ICONINFO ii;
			::GetIconInfo(icon, &ii);
			HBITMAP bitmap = ii.hbmMask;
			if (!bitmap) {
				bitmap = ii.hbmColor;
			}
			ATLASSERT (bitmap != NULL);
			BITMAP bmp;
			::GetObject(bitmap, sizeof(bmp), &bmp);
			ATLASSERT (bmp.bmHeight == ICON_SIZE && bmp.bmWidth == ICON_SIZE);
		}
#endif

		m_icon = icon;

		onSetIcon();

		if (IsWindow()) {
			UpdateWindow();
		}
	}
}

void CTransparentStatic::drawContent(HDC hdc) {
	CDCHandle dc(hdc);

	CRect rc, rcClient;
	GetClientRect(rcClient);
	int oldMode = dc.SetBkMode(TRANSPARENT);
	HFONT font = GetFont();
	HFONT oldFont = dc.SelectFont(font);
	COLORREF oldColor = dc.SetTextColor(m_rgbText);

	rc = rcClient;
	if (m_icon) {
		if (m_align == LEFT) {
			rc.left += ICON_SIZE + ICON_PADDING;
		} else if (m_align == CENTER) {
			rc.left += (ICON_SIZE + ICON_PADDING) / 2;
		}
	}
	
	UINT format = _GetDrawTextFormat();
	dc.DrawText(m_title.c_str(), m_title.length(), rc, format);

	if (m_icon) {
		dc.DrawText(m_title.c_str(), m_title.length(), rc, format | DT_CALCRECT);
		int x = 0;
		if (m_align == RIGHT) {
			x = rcClient.Width() - rc.Width() - ICON_SIZE - ICON_PADDING;
		} else if (m_align == CENTER) {
			x = rcClient.Width() - rc.Width() - ICON_SIZE - ICON_PADDING;
			x /= 2;
		}
		int y = (rcClient.Height() - ICON_SIZE) / 2;
		dc.DrawIconEx(x, y, m_icon, ICON_SIZE, ICON_SIZE);
	}
	
	dc.SetTextColor(oldColor);
	dc.SelectFont(oldFont);
	dc.SetBkMode(oldMode);
}

UINT CTransparentStatic::_GetDrawTextFormat() {

	UINT format = DT_VCENTER | DT_SINGLELINE | DT_NOPREFIX;
	if (m_align == CENTER) {
		format |= DT_CENTER;
	} else if (m_align == RIGHT) {
		format |= DT_RIGHT;
	} else {
		format |= DT_LEFT;
	}

	return format;
}

LRESULT CTransparentStatic::OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) {
	int len = GetWindowTextLength();
	if (len > 0) {
		TCHAR *buf = new TCHAR[++ len];
		if (buf) {
			GetWindowText(buf, len);
			m_title = buf;
			delete []buf;
		}
	}

	return TRUE;
}

LRESULT CTransparentStatic::OnDestroy(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) {
	bHandled = false;
	if (m_font) {
		::DeleteObject(m_font);
		m_font = NULL;
	}
	return 0;
}

LRESULT CTransparentStatic::OnSetText(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) {
	ATLASSERT (m_sibling);
	LRESULT res = TRUE;//DefWindowProc(uMsg, wParam, lParam);
	m_title = (LPCTSTR)lParam;

	onSetText();

	InvalidateRect(NULL);
	return res;
}

LRESULT CTransparentStatic::OnPaint(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) {

	if (1) {
		CRect rc;
		GetWindowRect(rc);
		ShowWindow(SW_HIDE);
		if (m_sibling) {
			m_sibling->ScreenToClient(rc);
			m_sibling->InvalidateRect(rc);
			m_sibling->UpdateWindow();
		} else {
			ATL::CWindow wndParent = GetParent();
			wndParent.ScreenToClient(rc);
			wndParent.InvalidateRect(rc);
			wndParent.UpdateWindow();
		}
		ShowWindow(SW_SHOWNA);
	}


	PAINTSTRUCT ps;
	HDC hdc = BeginPaint(&ps);

	drawContent(hdc);

	EndPaint(&ps);
	return TRUE;
}

LRESULT CTransparentStatic::OnEraseBkGnd(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) {
	return TRUE;
}

LRESULT CTransparentStatic::OnSetFont(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) {
	m_font = (HFONT)wParam;
	onSetFont();
	if (LOWORD(lParam) && IsWindow()) {
		UpdateWindow();
	}
	return 0;
}

LRESULT CTransparentStatic::OnGetFont(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) {
	return (LRESULT)m_font;
}

